﻿@using zy_simu.Data
@inject DeviceSnap dev_snap

<style>
    .cell_mark {
        --bs-table-bg: lightyellow;
    }
</style>

<h5>@title</h5>
<div style="font-size: 12px">
    <Table @ref="@StateTable" TItem="CtrlState" Items="@StateData.Take(StateData.Count)"
           IsStriped="true" IsBordered="true" Height="@_height" AllowResizing="true" IsFixedHeader="true"
           style="--bb-table-td-padding-x: .1rem; --bb-table-td-padding-y: .1rem"
           ClickToSelect="true" DoubleClickToEdit="true" OnSaveAsync="@OnSaveAsync">
        <TableColumns>
            <TableColumn @bind-Field="@context.name" Text="名称" Align="Alignment.Center" Readonly="true" />
            <TableColumn @bind-Field="@context.value" Text="状态值" Width="60" Align="Alignment.Center" OnCellRender="@OnCellRenderHandler" />
            <TableColumn @bind-Field="@context.des" Text="描述" Align="Alignment.Center" Readonly="true" />
        </TableColumns>
    </Table>
</div>


@code {
    private string title = "CtrlStateTable";
    static string[] title_state = { "Off", "Normal", "Prepare LowPower", "LowPower", "LowPower - 24H Report", "SA", "FA", "SW-RESET", "SW-RESET-Ctrl", "MODE_SWITCH", "SW-RESET-HD", "CTRL_STATE_DIAG_1004" };
    public class CtrlState
    {
        public string name { get; set; } = "";
        public int value { get; set; } = 0;
        public string des { get; set; } = "";

        public int trace; // 追踪修改的时间, 修改后10s内高亮显示。
    }

    public Table<CtrlState>? StateTable { get; set; } // table 控件
    public List<CtrlState> StateData { get; set; } = new List<CtrlState>();

    protected override void OnInitialized()
    {
        int cnt = dev_snap.st_name.Count() - 1;
        for (int i = 0; i < cnt; i++)
        {
            CtrlState item = new();
            item.name = dev_snap.st_name[i];
            item.value = 0;
            item.des = dev_snap.st_des[i];
            item.trace = 1000 * 1000;
            StateData.Add(item);
        }
        base.OnInitialized();
    }

    // 父组件周期调用，更新数据
    public void UpdateData(ZyStates st)
    {
        int flag = 0;
        int cnt = dev_snap.st_name.Count() - 1;
        // modified_idx = -1; // clear modified data index
        System.Reflection.FieldInfo[] fields = st.GetType().GetFields();
        for (int i = 0; i < cnt; i++)
        {   
            int value = (int)Convert.ChangeType(fields[i].GetValue(st), typeof(int));
            if (StateData[i].value != value)
            {
                StateData[i].value = value;
                StateData[i].trace = 0;
                flag = 1;
            }
            if (StateData[i].trace < 10000)
                StateData[i].trace += 50;
            else if (StateData[i].trace == 10000)
                flag = 1;
        }

        title = String.Format("CtrlStateTable - {0}", title_state[st.ctrl_state]);
        if (flag == 1)
            InvokeAsync(StateHasChanged);
    }

    // 数据变化, mark背景黄色10s以作提示
    private static void OnCellRenderHandler(TableCellArgs args)
    {
        if (args.Row is CtrlState st && args.ColumnName == "value")
        {
            if (st.trace < 10000)
            {
                args.Value = $"{st.value}";
                args.Class = "cell_mark";
            }
        }
    }

    public int modified_idx = -1;
    public int modified_value = 0;
    // 自身改变数据, 记录
    private Task<bool> OnSaveAsync(CtrlState item, ItemChangedType changedType)
    {
        if (changedType == ItemChangedType.Update)
        {
            modified_idx = -1;
            int cnt = StateData.Count();
            for (int i = 0; i < cnt; i++)
            {
                if (StateData[i].name == item.name)
                {
                    modified_value = item.value;
                    modified_idx = i;
                    break;
                }
            }
        }
        return Task.FromResult(true);
    }

    private int _height = 550;
    public void SetHeight(int height)
    {
        _height = height;
        StateHasChanged();
    }
}
